package com.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Api(tags = "邮件发送通用接口")
@RestController
public class EmailController {
    @Autowired
    private JavaMailSender jms;

    @Value("$(spring.mail.username)")
    private String username;

    @RequestMapping(value = "/SendEmail", method = RequestMethod.POST)
    public String SendEmail(String recipient, String title, String text) {
        System.out.println("进入发送普通邮件的方法");
        String str = null;
        try {
            SimpleMailMessage message = new SimpleMailMessage();

            message.setFrom(username);
            message.setTo(recipient);
            message.setSubject(title);
            message.setText(text);

            jms.send(message);
            str = "邮件发送成功";
        } catch (Exception e) {
            System.out.println("邮件发送失败！原因：" + e.getMessage());
            str = "发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendHtmlEmail", method = RequestMethod.POST)
    public String SendHtmlEmail(String recipient, String title, String text) {
        System.out.println("进入发送html内容的邮件方法");
        String str = null;
        try {
            MimeMessage message = null;
            message = jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message, true);
            html.setFrom(username);
            html.setTo(recipient);
            html.setSubject(title);

            StringBuffer sb = new StringBuffer(text);
            html.setText(sb.toString(), true);

            jms.send(message);
            str = "html邮件发送成功";
        } catch (MessagingException e) {
            System.out.println("html邮件发送失败！原因：" + e.getMessage());
            str = "html发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendFuJian", method = RequestMethod.POST)
    public String SendFuJian(String recipient, String title, String text, String path, String filename) {
        System.out.println("进入发送附件内容的邮件方法");
        String str = null;
        try {
            MimeMessage message = null;
            message = jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message, true, "UTF-8");
            html.setFrom(username);
            html.setTo(recipient);
            html.setSubject(title);
            html.setText(text);
            FileSystemResource file = new FileSystemResource(path);
            html.addAttachment(filename, file);

            jms.send(message);
            str = "附件邮件发送成功";
        } catch (MessagingException e) {
            System.out.println("附件邮件发送失败！原因：" + e.getMessage());
            str = "附件发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendImgEmail", method = RequestMethod.POST)
    public String SendImgEmail(String recipient, String title, String text, String path, String filename) {
        System.out.println("进入发送附件内容的邮件方法");
        String str = null;
        try {
            MimeMessage message = null;
            message = jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message, true, "UTF-8");
            html.setFrom(username);
            html.setTo(recipient);
            html.setSubject(title);
            html.setText("<html><body>" + text + "<img src='cid:img'/></body></html>");
            FileSystemResource fileImg = new FileSystemResource(path);
            html.addAttachment(filename, fileImg);

            jms.send(message);
            str = "图片邮件发送成功";
        } catch (MessagingException e) {
            System.out.println("图片邮件发送失败！原因：" + e.getMessage());
            str = "图片发送失败";
        }
        return str;
    }
}
